package com.weavernorth.action.application;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 销假流程每月4日后不可以提交上个月销假
 */
public class BackLeaveLimit implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        RecordSet rsDetail = new RecordSet();

        //获取请求ID
        String strLcId = requestInfo.getRequestid();
        bb.writeLog("requestid的值是："+strLcId);
        //获取表单名称
        //String strTableName= requestInfo.getRequestManager().getBillTableName();    RecordSet rsTable=new RecordSet();

        RecordSet rsTable=new RecordSet();
        String strTableName ="";
        rsTable.execute("select tablename  from workflow_bill where id = (select formid from workflow_base where id=(select workflowid from workflow_requestbase  where requestid='"+strLcId+"') and isbill=1)");
        if(rsTable.next()){
            strTableName=rsTable.getString("tablename");
        }
        bb.writeLog("tablename的值是："+strTableName);
        //获取表单id（workflowid）
        String strBdId = requestInfo.getWorkflowid();
        bb.writeLog("workflowid的值是："+strBdId);

        //格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前日期
        Date date = new Date();

        String formatDate = sdf.format(date);
        bb.writeLog("当前日期："+formatDate);
      //得到当前鱼的上个月最后一天
        String lastDay=getLastDayOfMonth(formatDate.substring(0,formatDate.length()-3));
        bb.writeLog("销假流程得到上个月的最后一天:"+lastDay);
        //当前 天(日子)
        int intCurrDay=Integer.parseInt(formatDate.split("-")[2]);
        bb.writeLog("销假的当前天:"+intCurrDay);



        //查询明细表数据的sql
        String strSqlDetail= "select * from "+strTableName+"_dt1 d,"+strTableName+" s  where d.mainid=s.id  and  s.requestid = '"+strLcId+"'";
        bb.writeLog("查询明细表数据的sql："+strSqlDetail);
        rsDetail.executeQuery(strSqlDetail);

        //判断是否有值
        while(rsDetail.next()){
            //获取开始日期
            String  strKsrq = Util.null2String(rsDetail.getString("detail_fromDate"));
            bb.writeLog("销假流程的开始日期:"+strKsrq);
            //销假结束日期
            String strJsrq=Util.null2String(rsDetail.getString("detail_toDate"));
            bb.writeLog("销假流程的日期:"+strJsrq);
            if(intCurrDay>4) {
                //判断当前销假日期是否是上个月的日期
                if (strKsrq.compareTo(lastDay) <=0 && strJsrq.compareTo(lastDay) >= 0) {
                    requestInfo.getRequestManager().setMessagecontent("每月4日后，不能提交上个月销假!");
                    return FAILURE_AND_CONTINUE;
                }
                if (strJsrq.compareTo(lastDay) <= 0) {
                    requestInfo.getRequestManager().setMessagecontent("每月4日后，不能提交上个月销假!");
                    return FAILURE_AND_CONTINUE;
                }
            }



        }








        return SUCCESS;
    }

 //获取当前月份的上个月的最后一天
    public static String getLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]);  //年
        int month = Integer.parseInt(yearMonth.split("-")[1]); //月
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // //设置当前月的上一个月
        cal.set(Calendar.MONTH, month - 1);
        //获取月份中的最小值，即第一天
        int lastDay = cal.getMinimum(Calendar.DATE);
        //上月的第一天减去1就是当月的最后一天
        cal.set(Calendar.DAY_OF_MONTH, lastDay - 1);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

}
